#ifndef _CROSSLIST_BASE_H_
#define _CROSSLIST_BASE_H_

#include <stdint.h>
#include <stdbool.h>

/* 十字链表 */
class CrossList
{
public:
    void *value;

    CrossList(void *val = NULL);
    virtual ~CrossList();
    CrossList(const CrossList&) = delete;

    /* 当前节点和 node 的关系 */
    bool IsInsideOf(CrossList *node);
    bool IsChildOf(CrossList *node);
    bool IsParentOf(CrossList *node);

    /* 添加节点,返回index,-1失败 */
    int32_t PushBack(CrossList *node);
    int32_t PushFront(CrossList *node);
    int32_t Insert(CrossList *node, int32_t index);

    int32_t PushBackChild(CrossList *node);
    int32_t PushFrontChild(CrossList *node);
    int32_t InsertChild(CrossList *node, int32_t index);

    /* 弹出节点 */
    CrossList* PopBackChild();
    CrossList* PopFrontChild();

    /* 从链表中移除自己(保留child) */
    void Remove();

    /* 同级链表查找 */
    CrossList* Parent();
    CrossList* Prev();
    CrossList* Next();
    int32_t Index();
    CrossList* At(int32_t index);

    /* 子链表链表查找 */
    int32_t ChildNum();
    CrossList* ChildBegin();
    CrossList* ChildEnd();
    CrossList* ChildAt(int32_t index);

    /* 互斥锁(按需要实现) */
    virtual void Lock();
    virtual void UnLock();

private:
    int32_t index = 0; /* 在当前横向链表中的序号,从0数起 */
    CrossList *prev = NULL;
    CrossList *next = NULL;
    CrossList *parent = NULL;

    int32_t childNum = 0; /* 子链表节点个数 */
    CrossList *firstChild = NULL; /* 指向子链表头 */
    CrossList *lastChild = NULL; /* 指向子链表尾 */
};

#endif // _CROSSLIST_BASE_H_
